#!/bin/sh
EXTRA_PARAM=""

NET_DEVS=`cat /proc/net/dev | sed -ne 's/^\([a-zA-Z0-9 ]*\):.*$/\1/p'`
NET_ADDR=`cat /proc/cmdline | sed -ne 's/^.*ip=\([^ :]*\).*$/\1/p'`

if [ -z "$NET_ADDR" ]; then
    NET_ADDR=`cat /proc/cmdline | sed -ne 's/^.*eth[0-9]:\([^ :]*\).*$/\1/p'`
fi

if [ ! -z "$NET_ADDR" ]; then
    if [ "$NET_ADDR" = dhcp ]; then
        ethn=`dmesg | grep "device=eth" | sed -e "s/^.*\(eth[0-9]\)\(.*\)/\1/"`
        if [ ! -z "$ethn" ]; then
	        EXTRA_PARAM="-I $ethn"
        fi
    else
        for i in $NET_DEVS; do
        ADDR=`ifconfig $i | sed 's/addr://g' | sed -ne 's/^.*inet \([0-9.]*\) .*$/\1/p'`
        if [ "$NET_ADDR" = "$ADDR" ]; then
            EXTRA_PARAM="-I $i"
            break
        fi
        done
    fi
fi

#if [ -f @DATADIR@/connman/wired-setup ] ; then
#	. @DATADIR@/connman/wired-setup
#fi

[ ! -d /run/connmand ] && mkdir -p /run/connmand
echo "CONNMAND_OPTS=$EXTRA_PARAM">/run/connmand/connmand.env
